const Actions = require('./actions');
const DB = require('app/db');
import { Toast } from 'saltui';
import { monStorage, Storage } from 'clientConfig/util/StoreData';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },

    onFetch: function (pageNum, props, success, error) {
        let t = this;
        DB.ParentNotice.get_notice_lists({
            schoolId: Storage.get('user.schoolId'),
            studentId: Storage.get('user.studentId'),
            pageIndex: pageNum
        })
            .then((content) => {
                let schoolId = content.check.schoolId;
                let studentId = content.check.studentId;
                content = content.data.dataList;
                var list = [];
                var obj = {};
                if (content && content.length > 0) {
                    for (let item of content) {
                        obj["schoolId"] = schoolId;
                        obj["studentId"] = studentId;
                        obj["badge"] = false;
                        obj["noticeId"] = item.noticeId;
                        obj["replayNum"] = item.replayNum;
                        obj["title"] = item.noticeTitle;
                        obj["regularlyFlag"] = item.regularlyFlag;
                        obj["releasePosition"] = item.releasePosition;
                        obj["noticeStudentId"] = item.noticeStudentId;
                        obj["noticeRang"] = item.noticeRang;
                        obj["oldNoticeTime"] = item.oldNoticeTime;
                        obj["noticeType"] = item.noticeType;
                        obj["isRead"] = item.isRead;
                        obj["subtitle"] = item.noticeTime;
                        obj["isreceived"] = true;
                        obj["content"] = [
                            { "subcontent": "", "unitcontent": item.noticeMsg }
                        ];
                        list.push(obj);
                        obj = {};
                    }
                }
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if (success){
                    success(hasNoMore, list);
                }else if (!content||!success) {
                    error();
                }       
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onDeleteMsg: function (cb) {
        let t = this;
        if (window.selectListItem && window.selectListItem instanceof Array) {
            DB.ParentNotice.del_receive_notice({
                noticeStudentIds: window.selectListItem.toString()
            }).then((content) => {
                if(content.code==='1'){
                    cb&&cb();
                    window.selectListItem = null;
                }
            }).catch((error) => { // 失败 or 有异常被捕获
                Toast.show({
                    type: 'error',
                    content: error
                });
            })
        } 
       
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
